package com.sentiment.algorithm;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.InputStreamReader;
import java.io.*;
import java.util.Arrays;

/**
 * Created by vani on 15-4-9.
 * self_LSH file handle
 */
public class SelfFile {
    private String inputFilenamePath;
    private String outputFilenamePath;

    private String inputDir;
    private String filterName;
    private BufferedReader in;
    public String filename;

    public String text;

    public SelfFile() {
    }

    public SelfFile(String filename) {
        this.filename=filename;
        initFileReader();
    }

    public void initFileReader(){
        try{
        	InputStreamReader isr = new InputStreamReader(new FileInputStream(filename), "UTF-8");
        	in = new BufferedReader(isr);
//            in=new BufferedReader(new FileReader(filename));

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public  boolean getpatternPara(){
        try{
            if((text=in.readLine())!=null){
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    public void ShowDirList(String[] args){
        File path=new File(inputDir);
        String[] list;
        if(args.length==0){
            list=path.list();
        }else {
            list=path.list(new DirFileter(args[0]));
        }
        Arrays.sort(list,new AlphaComparator());
        for(int i=0;i<list.length;i++){
            System.out.println(list[i]);
        }

    }

    public String[] ShowDirListPath(String dirPath){
        File path=new File(dirPath);
        String[] list=path.list();
        for (int i=0;i<list.length;i++){
        	System.out.println(list[i]);
            list[i]=dirPath+"//"+list[i];
        }
        return list;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    class DirFileter implements FilenameFilter{
        String afn;
        DirFileter(String afn){
            this.afn=afn;
        }
        public boolean accept(File dir, String name) {
            String f=new File(name).getName();
            return f.indexOf(afn)!=-1;
        }
    }


    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getInputFilenamePath() {
        return inputFilenamePath;
    }

    public void setInputFilenamePath(String inputFilenamePath) {
        this.inputFilenamePath = inputFilenamePath;
    }

    public String getOutputFilenamePath() {
        return outputFilenamePath;
    }

    public void setOutputFilenamePath(String outputFilenamePath) {
        this.outputFilenamePath = outputFilenamePath;
    }

    public String getInputDir() {
        return inputDir;
    }

    public void setInputDir(String inputDir) {
        this.inputDir = inputDir;
    }

    public String getFilterName() {
        return filterName;
    }

    public void setFilterName(String filterName) {
        this.filterName = filterName;
    }

    public static void main(String args[]){
        SelfFile selfFile=new SelfFile();
        selfFile.setInputDir("/home/vani/develop/idea-IU-133.696/bin");
        selfFile.setFilterName("l");
        args=new String[1];
        args[0]=selfFile.getFilterName();
        selfFile.ShowDirList(args);
        return;
    }

}
